package vip.xiaonuo.modular.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import vip.xiaonuo.core.consts.CommonConstant;
import vip.xiaonuo.core.enums.CommonStatusEnum;
import vip.xiaonuo.core.exception.ServiceException;
import vip.xiaonuo.core.factory.PageFactory;
import vip.xiaonuo.core.pojo.page.PageResult;
import vip.xiaonuo.modular.entity.Kaochang;
import vip.xiaonuo.modular.enums.KaochangExceptionEnum;
import vip.xiaonuo.modular.mapper.KaochangMapper;
import vip.xiaonuo.modular.param.KaochangParam;
import vip.xiaonuo.modular.service.KaochangService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.List;

/**
 * 考场管理service接口实现类
 *
 * @author 春非昔日春
 * @date 2023-02-28 13:16:01
 */
@Service
public class KaochangServiceImpl extends ServiceImpl<KaochangMapper, Kaochang> implements KaochangService {

    @Override
    public PageResult<Kaochang> page(KaochangParam kaochangParam) {
        QueryWrapper<Kaochang> queryWrapper = new QueryWrapper<>();
        if (ObjectUtil.isNotNull(kaochangParam)) {

            // 根据考场名称 查询
            if (ObjectUtil.isNotEmpty(kaochangParam.getName())) {
                queryWrapper.lambda().like(Kaochang::getName, kaochangParam.getName());
            }
            // 根据考场学校 查询
            if (ObjectUtil.isNotEmpty(kaochangParam.getPosition())) {
                queryWrapper.lambda().like(Kaochang::getPosition, kaochangParam.getPosition());
            }
            // 根据考场教室 查询
            if (ObjectUtil.isNotEmpty(kaochangParam.getClassroom())) {
                queryWrapper.lambda().like(Kaochang::getClassroom, kaochangParam.getClassroom());
            }
        }
        return new PageResult<>(this.page(PageFactory.defaultPage(), queryWrapper));
    }

    @Override
    public List<Kaochang> list(KaochangParam kaochangParam) {
        return this.list();
    }

    @Override
    public void add(KaochangParam kaochangParam) {
        Kaochang kaochang = new Kaochang();
        BeanUtil.copyProperties(kaochangParam, kaochang);
        this.save(kaochang);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(List<KaochangParam> kaochangParamList) {
        kaochangParamList.forEach(kaochangParam -> {
        Kaochang kaochang = this.queryKaochang(kaochangParam);
            this.removeById(kaochang.getId());
        });
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void edit(KaochangParam kaochangParam) {
        Kaochang kaochang = this.queryKaochang(kaochangParam);
        BeanUtil.copyProperties(kaochangParam, kaochang);
        this.updateById(kaochang);
    }

    @Override
    public Kaochang detail(KaochangParam kaochangParam) {
        return this.queryKaochang(kaochangParam);
    }

    /**
     * 获取考场管理
     *
     * @author 春非昔日春
     * @date 2023-02-28 13:16:01
     */
    private Kaochang queryKaochang(KaochangParam kaochangParam) {
        Kaochang kaochang = this.getById(kaochangParam.getId());
        if (ObjectUtil.isNull(kaochang)) {
            throw new ServiceException(KaochangExceptionEnum.NOT_EXIST);
        }
        return kaochang;
    }
}
